package org.tang.springcloudvv.base;

import org.springframework.util.StringUtils;
import org.tang.springcloudvv.constant.Constant;

import java.net.InetAddress;

public class AppContext {
    public static String app_environment = "";
    public static String app_name = "";
    public static String server_port = "";
    public static String startupTime = "";


    private static String hostIp=null;//服务器ip

    public static String string()
    {
        StringBuilder sb = new StringBuilder();
        sb.append("app_name="+app_name+"; ");
        sb.append("app_environment="+app_environment+"; ");
        sb.append("isProduct="+isProduct()+"; ");
        sb.append("server_port="+server_port+"; ");
        sb.append("hostIp="+hostIp+"; ");
        sb.append("startupTime="+startupTime+"; ");
        return sb.toString();
    }

    /**
     * 本地环境、开发环境、测试环境，都当做test环境，返回true
     * @return
     */
    public static  boolean isTest()
    {
        return !isProduct();
    }

    /**
     * 如果是生产环境，返回true
     * @return
     */
    public static boolean isProduct()
    {
        if (app_environment != null && app_environment.contains(Constant.APP_ENVIRONMENT_PRODUCTION))
        {
            return true;
        }
        return false;
    }

    public static String getLocalHostIp() {
        try {
            if (!StringUtils.hasLength(hostIp))
            {
                hostIp = InetAddress.getLocalHost().getHostAddress();
            }
        } catch (Exception e) {
            hostIp = "";
        }
        return hostIp;
    }


}
